package com.business.form.controller.gen;

import com.business.form.model.auto.FormInstance;
import com.business.form.service.FormInstanceFieldValueService;
import com.business.form.service.FormInstanceService;
import com.business.system.common.base.BaseController;
import com.business.system.common.domain.AjaxResult;
import com.business.system.model.custom.TableSplitResult;
import com.business.system.model.custom.Tablepar;
import com.business.system.model.custom.TitleVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("FormInstanceController")
@Api(value = "表单实例")
public class FormInstanceController extends BaseController{
	
	private String prefix = "gen/formInstance";
	@Autowired
	private FormInstanceService formInstanceService;
	@Autowired
	private FormInstanceFieldValueService formInstanceFieldValueService;
	
	@GetMapping("view")
    public String view(ModelMap model)
    {	
		String str="表单实例";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }
	
	//@Log(title = "表单实例集合查询", action = "111")
	@PostMapping("list")
	@ResponseBody
	public Object list(Tablepar tablepar,String searchTxt){
		PageInfo<FormInstance> page=formInstanceService.list(tablepar,searchTxt) ; 
		TableSplitResult<FormInstance> result=new TableSplitResult<FormInstance>(page.getPageNum(), page.getTotal(), page.getList()); 
		return  result;
	}
	
	/**
     * 新增
     */

    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }
	
	//@Log(title = "表单实例新增", action = "111")
	@PostMapping("add")
	@ResponseBody
	public AjaxResult add(FormInstance formInstance){
		int b=formInstanceService.insertSelective(formInstance);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 删除用户
	 * @param ids
	 * @return
	 */
	//@Log(title = "表单实例删除", action = "111")
	@PostMapping("remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=formInstanceService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 检查用户
	 * @param tsysUser
	 * @return
	 */
	@PostMapping("checkNameUnique")
	@ResponseBody
	public int checkNameUnique(FormInstance formInstance){
		int b=formInstanceService.checkNameUnique(formInstance);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}
	
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("FormInstance", formInstanceService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }
	
	/**
     * 修改保存
     */
    //@Log(title = "表单实例修改", action = "111")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FormInstance record)
    {
        return toAjax(formInstanceService.updateByPrimaryKeySelective(record));
    }

	/**
	 * 根据表单id和诉求id查询出对应的表单实例的字段值
	 * @param formId
	 * @param appealInfoId
	 * @return
	 */
    /*@GetMapping("selectFormInstance/{formId}/{appealInfoId}")
	@ResponseBody
    public AjaxResult seletFormInstance(@PathVariable("formId") String formId,@PathVariable("appealInfoId") String appealInfoId ){
		FormInstance instance = formInstanceService.seletFormInstance(formId,appealInfoId);
		if(instance!= null){
			List<FormInstanceFieldValue> formInstanceFieldValues =
					formInstanceFieldValueService.selectformInstanceFieldValueByInstanceId(instance.getId());
			return AjaxResult.successData(200,formInstanceFieldValues);
		}
		return AjaxResult.successData(200,null);
	}
*/
	
}
